//查询数据
db.getCollection("student")
db.getCollection("student").find()

//查询数据
db.student
db.student.find()

//插入一条数据
db.student.insertOne({"name":"niha"})
db.student.insertOne({"hobby":"niha"})

//插入多条数据
db.student.insertMany([{"name":"niha112321"},{"name":"niha123123"}])


//准备数据
//成绩集合
db.grades.insertMany([
{"stu_no":1001,"stu_name":"李明","sex":"男","class_name":"三年级一班","address":"希望小区","grade":{"course_name":"语文","teacher":"王老师","score":98},"phone_no":13300000000,"age":11},
{"stu_no":1002,"stu_name":"李明2","sex":"男","class_name":"三年级二班","address":"希望小区1","grade":{"course_name":"语文","teacher":"王老师","score":90},"phone_no":13300000000,"age":12},
{"stu_no":1003,"stu_name":"李明3","sex":"男","class_name":"三年级三班","address":"希望小区2","grade":{"course_name":"语文","teacher":"王老师","score":70},"phone_no":13300000000,"age":13}
])
//学生集合
db.students.insertMany([
{"stu_no":1001,"stu_name":"李明","sex":"男","class_name":"三年级一班","address":"希望小区","grades":[{"course_name":"语文","teacher":"王老师","score":98},{"course_name":"数学","teacher":"李老师","score":98},{"course_name":"英语","teacher":"吴老师","score":98}],"age":11},
{"stu_no":1002,"stu_name":"李明1","sex":"男","class_name":"三年级二班","address":"希望小区1","grades":[{"course_name":"语文","teacher":"王老师","score":90},{"course_name":"数学","teacher":"李老师","score":88},{"course_name":"英语","teacher":"吴老师","score":78}],"age":12},
{"stu_no":1003,"stu_name":"李明2","sex":"男","class_name":"三年级三班","address":"希望小区2","grades":[{"course_name":"语文","teacher":"王老师","score":70},{"course_name":"数学","teacher":"李老师","score":78},{"course_name":"英语","teacher":"吴老师","score":68}],"age":13}
])


//查询一条数据
db.students.findOne()
//选择哪些字段展示 1是展示，0是不展示
db.students.findOne({}, {"name":1,"age":1,"_id":0})

//查询多条数据（pretty()格式化显示，带缩进）
db.students.find({}).pretty()

//条件查询（比较运算符、多条件查询、在数组内查询）
//查询年龄大于12岁的所有学生
db.students.find({age:{$gt:12}})
//-- 查询年龄在9~12之间（含）的学生信息
db.students.find({age:{$gte:9,$lte:12}})
//-- 查询未设置/已设置年龄的学生信息
db.students.find({age:null})
db.students.find({age:{$ne:null}})
//-- 查询学生年龄9岁和12岁的学生信息
db.students.find({age:{$in:[9,12]}})

//逻辑运算符
//-- 查询所有12岁以上的男生和9岁以下的女生
db.students.find({
	$or:[
		{sex:'男',age:{$gt:12}},
		{sex:'女',age:{$lt:9}}
	]
})

//-- 查询所有姓“李”的学生信息
db.students.find({stu_name:/^李/})
//-- 查询所有姓“李”，名只有一个字的学生信息
db.students.find({stu_name:/^李.$/})
//-- 查找姓名中包含“雪”字的学生信息
db.students.find({stu_name:/雪/})


//嵌套文档查询
//查询所有学生的语文成绩
//grades中的grade字段嵌套的course_name
db.grades.find({"grade.course_name":"语文"})
//查找所有学生中语文成绩已经及格的学生信息
db.grades.find({"grade.course_name":"语文","grade.score":{$gte:60}})
db.grades.find({
	$and:[
		{"grade.course_name":"语文"},
		{"grade.score":{$gte:60}}
	]
})

//在数组内查询 （操作符：$all,$elemMatch,$size）
//查找只有两门成绩的学生信息
//students集合中的grades字段
db.students.find({grades:{$size:2}})




//聚合统计查询
//统计学生总人数
db.students.count()
db.students.find({grades:{$size:2}}).count()
db.students.find({grades:{$size:3}}).count()

//去重
//查找所有学生的居住区域
db.students.distinct("address")
db.students.distinct("address",{class_name:"三年级一班"})

//aggregate的用法
//-- 统计本班语文成绩的最高分/最低分
db.grades.aggregate([
//where
	{
		$match:{"grade.course_name":"语文"}
	},
//	group by
	{
	$group:{
		_id:null,
		maxScore:{$max:"$grade.score"},
		minScore:{$min:"$grade.score"},
		avgScore:{$avg:"$grade.score"},
	}
	}
])

//-- 统计学生李明的各科总分
db.grades.aggregate([
//where
	{
		$match:{"stu_name":"李明"}
	},
//group by
	{
	$group:{
		_id:null,
		totalScore:{$sum:"$grade.score"},
	}
	}
])

//-- 统计每个班的学生人数
db.students.aggregate([
	{
		$group:{
			_id:"$class_name",
			total:{$sum:1}
		}
	}
])

//统计每个班男生、女生的总人数
db.students.aggregate([
	{
		$group:{
			_id:{
				class_name:"$class_name",
				sex:"$sex"
			},
			total:{$sum:1}
		}
	}
])

//查找三门成绩都及格（>=60）的学生
db.students.aggregate([
//where
{
	$match:{"grades.score":{$gte:60}}
},
//group by
{
	$group:{
		_id:"$stu_no",
		total:{$sum:1}
	}
},
//having
{
	$match:{
		total:{$eq:3}
	}
}
])


//排序和分页
//将学生的语文成绩从高到低排序
db.grades.find({"grade.course_name":"语文"}).sort({"grade.score":-1})
//将学生的语文成绩按照年龄和成绩排序
db.grades.find({"grade.course_name":"语文"}).sort({"age":-1,"grade.score":-1})
//获取第3页学生成绩信息，每页8条
db.grades.find().skip(2).limit(2)
db.grades.find({"grade.course_name":"语文"}).sort({"grade.score":-1}).skip(2).limit(2)




//更新数据
//updateOne()
//replaceOne()
//updateMany()
//用户使用的student表
//设置用户的年龄为20岁
db.student.updateMany({},{$set:{age:20}})
db.student.find()
//删除用户的爱好和公司信息(实际删除的是created_at)
db.student.updateMany({}, {$unset:{created_at:null}})
//添加用户created_at为系统当前时间
db.student.updateMany({},{$currentDate:{created_at:true}})
//将用户的年龄+1
db.student.updateMany({}, {$inc:{age:1}})
//updateOne
db.student.updateOne({}, {$inc:{age:1}})
//replaceOne (替换文档)
db.student.replaceOne({}, {'a':1})

//新增一列
db.student.updateMany(
   {},
   { $set: { "company": "baidu" } }
);
//删除一列
db.student.updateMany({}, {$unset:{company:null}})





//删除数据
db.student.find({})
//db.student.deleteOne(filter, options)
//db.student.deleteMany(filter, options)
//db.student.findOneAndDelete(filter, options)
//db.student.findOneAndReplace(filter, replacement, options)

//批量删除数据
db.student.deleteMany({name:null})

//删除一条数据
db.student.deleteOne({name:"niha"})

//危险操作：删除集合中所有的内容
db.student.deleteMany({})

